package com.ciei.dpagm.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.ChipControlMode;
import com.ciei.dpagm.mapper.ChipControlModeMapper;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.JsonUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangyan
 * @date 2023/05/08
 */

@Service
public class ChipControlModeService extends BaseService<ChipControlModeMapper, ChipControlMode> {

    /**
     * 获取下拉框列表
     * @param webData
     * @return
     */
    public JSONObject getOptionList(JSONObject webData){
        Integer driverLibraryId = webData.getInteger("driverLibraryId");
        List<ChipControlMode> chipControlModeList = findByProperty(ChipControlMode::getDriverLibraryId, driverLibraryId);
        List<JSONObject> dataList = new ArrayList<>();
        for(ChipControlMode chipControlMode : chipControlModeList){
            JSONObject data = new JSONObject();
            String name = chipControlMode.getName() == null ? "" : chipControlMode.getName();
            data.fluentPut("value",name)
                    .fluentPut("label",name);
            dataList.add(data);
        }
        return JsonUtil.getSuccess("").fluentPut(APP.DATA,dataList);
    }

    /**
     * 获取列表
     * @param chipIdList
     * @return
     */
    public List<ChipControlMode> getList(List<Integer> chipIdList){
        if(chipIdList.isEmpty()){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ChipControlMode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ChipControlMode::getChipId,chipIdList);
        return list(lambdaQueryWrapper);
    }

    /**
     * 获取某个控制模式记录
     * @param driverLibraryId
     * @param controlModeName
     * @return
     */
    public ChipControlMode getOneByProperties(Integer driverLibraryId,String controlModeName){
        LambdaQueryWrapper<ChipControlMode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChipControlMode::getDriverLibraryId,driverLibraryId);
        lambdaQueryWrapper.eq(ChipControlMode::getName,controlModeName);
        return list(lambdaQueryWrapper).stream().findFirst().orElse(null);
    }

}
